﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>分类管理</h2>
    <input type="button" id="Insert_Btn" value="在选中区域下插入新区域" class="button" />
    <input type="button" id="Edit_Btn" value="修改选中区域" class="button" />
    <input type="button" id="Delete_Btn" value="删除选中区域" class="button" />
    <div id="PCategoryTree" style="padding-bottom: 10px">
        <p>
            <%= ViewData["CategoryData"]  %></p>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ScriptContent" runat="server">
    <script src="../../Scripts/JQTree/jquery.tree.js" type="text/javascript"></script>
    <link href="../../Scripts/JQTree/themes/checkbox/style.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/JQTree/plugins/jquery.tree.checkbox.js" type="text/javascript"></script>
    <script>
        $.tree.plugins.checkbox.check = function (n) {
            if (!n) return false;
            var t = $.tree.reference(n);
            n = t.get_node(n);
            if (n.children("a").hasClass("checked")) return true;
            n.children("a").removeClass("unchecked undetermined").addClass("checked");
            return true;
        }




        $(function () {
            var _CategoryTree = {
                OpenPCategoryTree: function () {
                    $.tree.reference("#PCategoryTree").open_all();
                },
                ClosePCategoryTree: function () {
                    $.tree.reference("#PCategoryTree").close_all();
                },
                CreatePCategoryTree: function () {
                    var values = {};
                    $("#PCategoryTree").tree({
                        data: {
                            async: true, 	// Are async requests used to load open_branch contents
                            type: "html", 	// One of included datastores
                            opts: { method: "GET", url: false} // Options passed to datastore
                        },
                        types: {
                            "default": {
                                draggable: true
                            }
                        },
                        ui: {
                            animation: 150,
                            theme_name: "checkbox"
                        },
                        plugins: {
                            checkbox: {}
                        },
                        callback: {
                            onsearch: function (n, t) {
                                $.each(n, function (i, o) {
                                    $(o).addClass('search');
                                })
                            }
                        }

                    });
                }
            }
            _CategoryTree.CreatePCategoryTree();
            _CategoryTree.OpenPCategoryTree(); //展开树


            $("#Insert_Btn").click(function () {
                var select = $.tree.plugins.checkbox.get_checked($.tree.reference("#PCategoryTree"));
                var flag;
                var _ucategory = '';
                $.each(select, function (i, n) {
                    _ucategory += n.id + '|';
                })
                _ucategory = _ucategory.substring(0, _ucategory.length - 1);
                if (select.length > 0) {
                    if (select.length > 1) {
                        alert("只能选择一项"); return false;
                    }
                    else {
                        location.href = "/HelperCenterCategory/create?pid=" + _ucategory;
                    }
                } else {
                    alert("请选择一个区域，它将作为新增区域的父区域")
                }
            });


            $("#Edit_Btn").click(function () {
                var select = $.tree.plugins.checkbox.get_checked($.tree.reference("#PCategoryTree"));
                var flag;
                var _ucategory = '';
                $.each(select, function (i, n) {
                    _ucategory += n.id + '|';
                })
                _ucategory = _ucategory.substring(0, _ucategory.length - 1);
                if (select.length > 0) {
                    if (select.length > 1) {
                        alert("只能选择一项"); return false;
                    }
                    else {
                        location.href = "/HelperCenterCategory/Edit?id=" + _ucategory;
                    }
                } else {
                    alert("请选择一个区域")
                }
            });



            //删除
            $("#Delete_Btn").click(function () {
                var _ucategory = '';
                var flag = true;
                $.each($.tree.plugins.checkbox.get_checked($.tree.reference("#PCategoryTree")), function (i, n) {
                    if (n.id == "0") {
                        flag = false;
                    }
                    _ucategory += n.id + '|';
                })
                if (_ucategory.length > 0) {
                    debugger;
                    if (flag) {
                        location.href = "/HelperCenterCategory/Delete?id=" + _ucategory;
                    }
                    else {
                        alert("删除节点中包含根节点，此节点不能删除！");
                    }
                }
                else {
                    alert("请选择要删除的项目分类");
                }
            })
        });

    </script>
</asp:Content>
